package com.tvunetworks.device.extra.controller;

import com.netflix.discovery.EurekaClient;
import com.tvunetworks.center.common.util.Result;
import com.tvunetworks.center.common.util.ResultUtil;
import com.tvunetworks.device.extra.service.AnywhereService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PreDestroy;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Etan Yang
 * @create: 2021/11/23 15:21
 */

@Slf4j
@RestController
@RequestMapping("/base")
public class BaseController {

    @Value("${buildversion:1.0.1}")
    private String buildversion;
    @Value("${commonBuildversion:build127}")
    private String commonVersion;
    @Value("${device.extra.url}")
    private String deviceExtraUrl;

    @Autowired
    private AnywhereService anywhereService;
    @Autowired
    private EurekaClient eurekaClient;


    /**
     * 获取后端版本
     * @return 后端版本信息
     */
    @GetMapping("version")
    public String version(){
        return buildversion + "|" + commonVersion;
    }

    /**
     * 初始化接口时前端获取配置文件
     * @return 配置文件
     */
    @GetMapping("initConfig")
    public Result<Object> initConfig() {
        Map map = new HashMap<>();
        map.put("deviceExtraUrl", deviceExtraUrl);
        return ResultUtil.success(map);
    }


    /**
     * token pair confirm
     *
     * @param param TokenPairConfirmParam
     * @return result
     */
    @PostMapping("pairConfirm")
    public Result pairConfirm(@RequestBody Map<String,Object> param) {
        return anywhereService.pairConfirm(param);
    }

    /**
     * 停止eureka
     */
    // kill -9 不触发

    /**
     * stop
     */
    @PreDestroy
    public void stop() {
        if (eurekaClient != null) {
            log.error("Shutting down server and notify to Eureka.");
            eurekaClient.shutdown();
        }
    }

}
